/*
Miranda-IM SMS Plugin
Copyright (C) 2001-2  Richard Hughes

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
---------------------------------------------------------------------------

This was the original words.
This plugin was modified by Ariel Shulman (NuKe007).
For any comments, problems, etc. contact me at Miranda-IM forums or E-Mail or ICQ.
All the information needed you can find at http://flashmagicd.com/projects/smsplugin/
Enjoy the code and use it smartly!
*/

#include "recvdlg.h"

//Defnition needed to the SMS window list

static SMSWindowRecv *SMSWindowRecvList=NULL;
static int SMSWindowRecvNum;


//END of defenitions


static WNDPROC OldEditWndProc;

static LRESULT CALLBACK MessageSubclassProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
	switch(message) {
		case WM_CHAR:
			if(wParam==_T('\n') && GetKeyState(VK_CONTROL)&0x8000) {
				PostMessage(GetParent(hwnd),WM_COMMAND,IDOK,0);
				return 0;
			}
			break;
	}
	return CallWindowProc(OldEditWndProc,hwnd,message,wParam,lParam);
}

INT_PTR CALLBACK RecvSmsDlgProc(HWND hwndDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
	static HFONT hFont;
	switch(message) {
		case WM_INITDIALOG:
			TranslateDialogDefault(hwndDlg); //Translate intially - bid
			SendMessage(hwndDlg,WM_SETICON,ICON_BIG,(LPARAM)LoadIcon(GetPluginhInst(),MAKEINTRESOURCE(IDI_SMS)));
			OldEditWndProc=(WNDPROC)SetWindowLongPtr(GetDlgItem(hwndDlg,IDC_MESSAGE),GWLP_WNDPROC,(LONG_PTR)MessageSubclassProc);
			InvalidateRect(GetDlgItem(hwndDlg,IDC_MESSAGE),NULL,FALSE);
			{	
				LOGFONT lf;
				hFont=(HFONT)SendDlgItemMessage(hwndDlg,IDC_MESSAGE,WM_GETFONT,0,0);
				if(hFont!=NULL && hFont!=(HFONT)SendDlgItemMessage(hwndDlg,IDOK,WM_GETFONT,0,0)) DeleteObject(hFont);
				LoadMsgDlgFont(MSGFONTID_YOURMSG,&lf,NULL);
				hFont=CreateFontIndirect(&lf);
				SendDlgItemMessage(hwndDlg,IDC_MESSAGE,WM_SETFONT,(WPARAM)hFont,MAKELPARAM(TRUE,0));
			}
			{
				int x,y,cx,cy;
				HANDLE hContact;
				hContact=NULL;
				x=DBGetContactSettingDword(hContact,SMSPLUGIN,"recvx",0);
				y=DBGetContactSettingDword(hContact,SMSPLUGIN,"recvy",0);
				cx=DBGetContactSettingDword(hContact,SMSPLUGIN,"recvwidth",0);
				cy=DBGetContactSettingDword(hContact,SMSPLUGIN,"recvheight",0);
				if((x==0) && (y==0) && (cx==0) && (cy==0))
					SetWindowPos(hwndDlg,0,200,200,400,350,SWP_NOZORDER);	
				else
					SetWindowPos(hwndDlg,0,x,y,cx,cy,SWP_NOZORDER);	
			}
			break;
		case WM_CTLCOLORSTATIC:
			{	
				COLORREF colour;
				HBRUSH hBkgBrush;
				if((HWND)lParam!=GetDlgItem(hwndDlg,IDC_MESSAGE)) break;
				LoadMsgDlgFont(MSGFONTID_YOURMSG,NULL,&colour);
				SetTextColor((HDC)wParam,colour);
				SetBkColor((HDC)wParam,DBGetContactSettingDword(NULL,SMSPLUGIN,"BkgColour",SRMSGDEFSET_BKGCOLOUR));
				hBkgBrush=CreateSolidBrush(DBGetContactSettingDword(NULL,SMSPLUGIN,"BkgColour",SRMSGDEFSET_BKGCOLOUR));
				return (BOOL)hBkgBrush;
			}
		case WM_GETMINMAXINFO:
			{
				RECT rcWin,rcClient;
				GetClientRect(hwndDlg,&rcWin);
				GetClientRect(hwndDlg,&rcClient);
				((MINMAXINFO*)lParam)->ptMinTrackSize.x=345+((rcWin.right-rcWin.left)-(rcClient.right-rcClient.left)); 
				((MINMAXINFO*)lParam)->ptMinTrackSize.y=230+((rcWin.top-rcWin.bottom)-(rcClient.top-rcClient.bottom));
			}
			break;
		case WM_SIZE:
			{
				RECT rcWin,rcClient;
				int cx,cy;
				GetClientRect(hwndDlg,&rcClient);
				GetWindowRect(hwndDlg,&rcWin);
				cx = rcClient.right - rcClient.left;
				cy = rcClient.bottom - rcClient.top;
				SetWindowPos(GetDlgItem(hwndDlg,IDC_MESSAGE),0,0,0,cx - 4,cy - 82,SWP_NOZORDER|SWP_NOMOVE);
				SetWindowPos(GetDlgItem(hwndDlg,IDC_NAME),0,0,0,(cx*35)/100,20,SWP_NOZORDER|SWP_NOMOVE);
				SetWindowPos(GetDlgItem(hwndDlg,IDC_NUMBER),0,cx - (cx*35)/100 - 2,5,(cx*35)/100,20,SWP_NOZORDER);
				SetWindowPos(GetDlgItem(hwndDlg,IDC_ST_NUMBER),0,cx - (cx*35)/100 - 49,5,40,20,SWP_NOZORDER);
				SetWindowPos(GetDlgItem(hwndDlg,IDC_READNEXT),0,cx - 84, cy - 30,80,25,SWP_NOZORDER);
				SetWindowPos(GetDlgItem(hwndDlg,IDOK),0,cx/2 - 87,cy - 30,80,25,SWP_NOZORDER);
				SetWindowPos(GetDlgItem(hwndDlg,IDCANCEL),0,cx/2 + 7,cy - 30,80,25,SWP_NOZORDER);

				RedrawWindow(hwndDlg,NULL,NULL,RDW_FRAME|RDW_INVALIDATE);
			}
			break;
		case WM_COMMAND:
			switch(LOWORD(wParam)) {
				case IDOK:
					{
						TCHAR name[256];
						TCHAR number[256];
						HANDLE hContact;
						HWND hwndSendSms;
						GetDlgItemText(hwndDlg,IDC_NAME,name,sizeof(name));
						GetDlgItemText(hwndDlg,IDC_NUMBER,number,sizeof(number));
						number[lstrlen(number)]=_T('\0');
						hContact=CellularToHandle(strCellular(number));
						hwndSendSms=IsOtherInstanceHContact(hContact);
						if(hwndSendSms != NULL)
						{
							SetFocus(hwndSendSms);
						}
						else
						{
							TCHAR newtitle[256];
							hwndSendSms=AddSendSMSWindow(GetRecvSMSWindowHContact(hwndDlg));
							SetSendSMSWindowHContact(hwndSendSms,GetRecvSMSWindowHContact(hwndDlg));
							//SendDlgItemMessage(hwndSendSms,IDC_ADDRESS,CB_RESETCONTENT,0,0);
							SendDlgItemMessage(hwndSendSms,IDC_NAME,CB_ADDSTRING,0,(LPARAM)name);
							SendDlgItemMessage(hwndSendSms, IDC_NAME, CB_SETCURSEL, 0, 0);
							lstrcpy(newtitle,name);
							lstrcat(newtitle,_T(" - "));
							lstrcat(newtitle,TranslateT("Send SMS"));
							SetWindowText(hwndSendSms,newtitle);
							{
								int i;
								char idstr[256];
								TCHAR *cTmp;
								HANDLE hContact=NULL;
								DBVARIANT dbv;
								char *szProto;
								hContact=GetRecvSMSWindowHContact(hwndDlg);
								SendDlgItemMessage(hwndSendSms,IDC_ADDRESS,CB_RESETCONTENT,0,0);
								szProto = (char *) CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM) hContact, 0);
								if(!DBGetContactSettingTString(hContact,szProto,"Cellular",&dbv)) 
								{
									if(lstrlen(dbv.ptszVal)>4 && !lstrcmp(dbv.ptszVal+lstrlen(dbv.ptszVal)-4,_T(" SMS"))) 
									{
										dbv.ptszVal[lstrlen(dbv.ptszVal)-4]=_T('\0');
										cTmp=(TCHAR*)mir_alloc((lstrlen(dbv.ptszVal)+2)*sizeof(TCHAR));
										lstrcpy(cTmp,_T("+"));
										lstrcat(cTmp,strCellular(dbv.ptszVal));
										SendDlgItemMessage(hwndSendSms,IDC_ADDRESS,CB_ADDSTRING,0,(LPARAM)cTmp);
										mir_free(cTmp);
									}
									DBFreeVariant(&dbv);
								}
								for(i=0;;i++) 
								{
									wsprintfA(idstr,"MyPhone%d",i);
									if(DBGetContactSettingTString(hContact,"UserInfo",idstr,&dbv))
										break;
									wsprintfA(idstr,Translate("Custom %d"),i+1);
									if(lstrlen(dbv.ptszVal)>4 && !lstrcmp(dbv.ptszVal+lstrlen(dbv.ptszVal)-4,_T(" SMS"))) 
									{
										dbv.ptszVal[lstrlen(dbv.ptszVal)-4]=('\0');
										cTmp=(TCHAR*)mir_alloc((lstrlen(dbv.ptszVal)+2)*sizeof(TCHAR));
										lstrcpy(cTmp,_T("+"));
										lstrcat(cTmp,strCellular(dbv.ptszVal));
										SendDlgItemMessage(hwndSendSms,IDC_ADDRESS,CB_ADDSTRING,0,(LPARAM)cTmp);
										mir_free(cTmp);
									}
									DBFreeVariant(&dbv);
								}
							}
							//SendDlgItemMessage(hwndSendSms,IDC_ADDRESS,CB_ADDSTRING,0,(LPARAM)number);
							SetDlgItemText(hwndSendSms,IDC_ADDRESS,number);
							SetFocus(GetDlgItem(hwndSendSms,IDC_MESSAGE));
							SetSendSMSWindowHContact(hwndSendSms,hContact);
							//{
							//	int x,y,cx,cy;
							//	HANDLE hContact;
							//	if(DBGetContactSettingByte(NULL,"SRMsg","SavePerContact",0))
							//		hContact=GetSendSMSWindowHContact(hwndSendSms);
							//	else hContact=NULL;
							//	x = DBGetContactSettingDword(hContact,SMSPLUGIN,"sendx",0);
							//	y = DBGetContactSettingDword(hContact,SMSPLUGIN,"sendy",0);
							//	cx = DBGetContactSettingDword(hContact,SMSPLUGIN,"sendwidth",0);
							//	cy = DBGetContactSettingDword(hContact,SMSPLUGIN,"sendheight",0);
							//	if((x==0) && (y==0) && (cx==0) && (cy==0))
							//		SetWindowPos(hwndSendSms,0,200,200,400,350,SWP_NOZORDER);	
							//	else
							//		SetWindowPos(hwndSendSms,0,x,y,cx,cy,SWP_NOZORDER);
							//}
						}
						
					}
				case IDCANCEL:
					DeleteObject(hFont);
					RemoveRecvSMSWindow(hwndDlg);
					break;
			}
			break;
		case WM_CLOSE:
			DeleteObject(hFont);
			RemoveRecvSMSWindow(hwndDlg);
			break;
	}
	return FALSE;
}

//SMS Receive window list functions

//This function create a new SMS receive window, and insert it to the list.
//The function gets void and return the window HWND
HWND AddRecvSMSWindow()
{
	SMSWindowRecv SMSWindowRecvTmp;
	SMSWindowRecvTmp.hwndSMS = CreateDialog(GetPluginhInst(),MAKEINTRESOURCE(IDD_RECVSMS),NULL,RecvSmsDlgProc);
	SMSWindowRecvTmp.tMsg = NULL;
	SMSWindowRecvTmp.hContact = NULL;
	SMSWindowRecvNum++;
	SMSWindowRecvList=(SMSWindowRecv*)mir_realloc(SMSWindowRecvList,sizeof(SMSWindowRecv) * SMSWindowRecvNum);
	*(SMSWindowRecvList + SMSWindowRecvNum - 1) = SMSWindowRecvTmp;
	return SMSWindowRecvTmp.hwndSMS;
}

//This function set the contact info of the person we send him the in the given to the SMS receive window.
//The function gets the HWND of the window and the HANDLE of the contact and return void
void SetRecvSMSWindowHContact(HWND hwndDlg, HANDLE hContact)
{
	int i;
	for(i=0;i < SMSWindowRecvNum;i++)
		if((SMSWindowRecvList + i)->hwndSMS == hwndDlg)
		{
			(SMSWindowRecvList + i)->hContact = hContact;
			return;
		}
}

HANDLE GetRecvSMSWindowHContact(HWND hwndDlg)
{
	int i;
	for(i=0;i < SMSWindowRecvNum;i++)
		if((SMSWindowRecvList + i)->hwndSMS == hwndDlg)
		{
			return (SMSWindowRecvList + i)->hContact;
		}
	return NULL;
}

//This function close the SMS receive window that given, and remove it from the list.
//The function gets the HWND of the window that should be removed and return void
void RemoveRecvSMSWindow(HWND hwndRemove)
{
	int i,iDel;
	SMSWindowRecv SMSWindowRecvTmp;
	for(i=0;i < SMSWindowRecvNum;i++)
		if((SMSWindowRecvList + i)->hwndSMS == hwndRemove)
			break;
	iDel=i;
	{
		WINDOWPLACEMENT wp={0};
		HANDLE hContact;
		if(DBGetContactSettingByte(NULL,SMSPLUGIN,"SavePerContact",0))
			hContact=GetSendSMSWindowHContact(hwndRemove);
		else hContact=NULL;
		wp.length=sizeof(wp);
		GetWindowPlacement(hwndRemove,&wp);
		DBWriteContactSettingDword(hContact,SMSPLUGIN,"recvx",wp.rcNormalPosition.left);
		DBWriteContactSettingDword(hContact,SMSPLUGIN,"recvy",wp.rcNormalPosition.top);
		DBWriteContactSettingDword(hContact,SMSPLUGIN,"recvwidth",wp.rcNormalPosition.right-wp.rcNormalPosition.left);
		DBWriteContactSettingDword(hContact,SMSPLUGIN,"recvheight",wp.rcNormalPosition.bottom-wp.rcNormalPosition.top);
	}	
	DestroyWindow((SMSWindowRecvList + iDel)->hwndSMS);
	mir_free((SMSWindowRecvList + iDel)->tMsg);
	SMSWindowRecvNum--;
	SMSWindowRecvTmp = *(SMSWindowRecvList + SMSWindowRecvNum);
	if(SMSWindowRecvNum != 0)
		SMSWindowRecvList=(SMSWindowRecv*)mir_realloc(SMSWindowRecvList,sizeof(SMSWindowRecv)*SMSWindowRecvNum);
	else
		SMSWindowRecvList=NULL;
	if(iDel < SMSWindowRecvNum)
		*(SMSWindowRecvList + iDel) = SMSWindowRecvTmp;
}

//This function destroy all SMS receive windows
void RemoveAllRecvSMSWindow()
{
	int i;
	for(i=0;i < SMSWindowRecvNum;i++)
	{
		mir_free((SMSWindowRecvList + i)->tMsg);
		DestroyWindow((SMSWindowRecvList + i)->hwndSMS);
	}
	SMSWindowRecvNum=0;
	mir_free(SMSWindowRecvList);
	SMSWindowRecvList=NULL;
}

//
void AddRecvSMSWindowNewMessage(HWND hwndDlg, TCHAR *tMessage)
{
//	int i;
//	for(i=0;i < SMSWindowRecvNum;i++)
//		if((SMSWindowRecvList + i)->hwndSMS == hwndDlg)
//		{
//			(SMSWindowRecvList + i)->cMessagesNum++;
//		}
}

//
void ReadNextMessage(HWND hwndDlg)
{
	
}

//
void RemoveAllNextMessages(HWND hwndDlg)
{
	
}